<?php
namespace App\Http\Controllers\Api;

use App\Wen\Utils\Settings;
use App\Wen\Utils\UserUtils;
use App\Models\User\WxUserLuckDrawRecord;
use Illuminate\Http\Request;

class LuckDrawController extends BaseController{
    public function getLuckDrawInfo(Request $request){
        $uid = $request->uid;

        $freeNumDay = Settings::get('app_luck_draw_times', 3);
        $data = [
            'prizeList' => [],
            'userCoins' => UserUtils::user_coins($uid),
            'restFreeNum' => $freeNumDay - $this->getTodayLuckDrawTimes($uid, 4),
            'goldNumEvery' => Settings::get('app_luck_draw_coin_pay', 20),
            'freeNumDay' => $freeNumDay,
            'tips' => Settings::get('app_luck_draw_tips',[]),
            'background' => Settings::get('app_luck_draw_background', 'https://img.mini.minisns.cn/hbx/video/luckdrawtopbg.png')
        ];
        $app_luck_draw = Settings::get('app_luck_draw', []);
        if($app_luck_draw){
            foreach ($app_luck_draw as $item){
                $data['prizeList'][] = [
                    'prizeId'=> $item['id'],
                    'prizeName' => $item['name'],
                    'prizeStock' => 99999,
                    'prizeWeight' => $item['weight'],
                    'prizeImage' => $item['img']
                ];
            }
        }
        return $this->success($data);
    }

    public function userLuckDraw(Request $request){
        $uid = $request->uid;
        $freeNumDay = Settings::get('app_luck_draw_times', 3);
        $freeTimes = $this->getTodayLuckDrawTimes($uid, 4);
        if($freeNumDay - $freeTimes > 0){

            $mostLuckItem = $this->getMostLuck();
            UserUtils::user_gift($uid, 1, $mostLuckItem['type'], $mostLuckItem['num'], '抽奖奖励：'.$mostLuckItem['name'], 8, _array_key($mostLuckItem, 'product', 0) );
            $model = new WxUserLuckDrawRecord();
            $model->user_id = $uid;
            $model->pay = 4;
            $model->type = $mostLuckItem['type'];
            $model->num = $mostLuckItem['num'];
            $model->tip = $mostLuckItem['name'];
            $model->save();

            $data = [
                'userCoins' => UserUtils::user_coins($uid),
                'restFreeNum' => $freeNumDay - $this->getTodayLuckDrawTimes($uid, 4),
                'prizeId' => $mostLuckItem['id']
            ];

            return $this->success($data);

        }else{
            $coins = UserUtils::user_coins($uid);
            $goldNumEvery = Settings::get('app_luck_draw_coin_pay', 20);
            if($coins - $goldNumEvery >= 0){
                $r = UserUtils::update_user_coins($uid, 14, -$goldNumEvery, '金币抽奖：-'.$goldNumEvery);
                if($r){
                    $mostLuckItem = $this->getMostLuck();
                    UserUtils::user_gift($uid, 1, $mostLuckItem['type'], $mostLuckItem['num'], '抽奖奖励：'.$mostLuckItem['name'], 8,  _array_key($mostLuckItem, 'product', 0) );
                    $model = new WxUserLuckDrawRecord();
                    $model->user_id = $uid;
                    $model->pay = 0;
                    $model->type = $mostLuckItem['type'];
                    $model->num = $mostLuckItem['num'];
                    $model->tip = $mostLuckItem['name'];
                    $model->save();

                    $data = [
                        'userCoins' => UserUtils::user_coins($uid),
                        'restFreeNum' => $freeNumDay - $this->getTodayLuckDrawTimes($uid, 4),
                        'prizeId' => $mostLuckItem['id']
                    ];

                    return $this->success($data);
                }else{
                    return $this->fail(200011);
                }
                // 金币付费
            }else{
                // 金币不足
                return $this->fail(200011);
            }
        }
    }

    public function getMostLuck(){
        $luck_draw = Settings::get('app_luck_draw', []);
        $total_weight = 0;
        foreach ($luck_draw as $item){
            $total_weight += $item['weight'];
        }
        $rand = mt_rand(1, $total_weight);
        $current = 0;
        foreach ($luck_draw as $item){
            $current += $item['weight'];
            if($current >= $rand){
                return $item;
            }
        }
        return null;
    }


    public function getTodayLuckDrawTimes($uid, $pay_type = -1){
        if($pay_type == -1){
            return WxUserLuckDrawRecord::where('user_id', $uid)
                ->where('created_at', 'like', '%' . date("Y-m-d", time()) . '%')
                ->count();
        }else{
            return WxUserLuckDrawRecord::where([['user_id', '=', $uid], ['pay', '=', $pay_type]])
                ->where('created_at', 'like', '%' . date("Y-m-d", time()) . '%')
                ->count();
        }
    }

}
